import { request, useUserStore, clearEmpty } from "jmash-core";
import qs from "qs";
import type { AxiosPromise } from "axios";
import type {
  DictEntryApi,
  DictEntryReq,
  DictEntryModel,
  DictEntryPage,
  DictEntryList,
  DictEntryKey,
  DictEntryKeyList,
  DictEntryMoveKey,
  DictEntryEnableKey,
  DictEntryCreateReq,
  DictEntryUpdateReq,
  DictEntryExportReq,
} from "./types";
import { dictTypeApi } from "@/api/dict/os-dict-type/index";
import type {
  DictTypeExportReq,
  DictTypeImportReq,
} from "@/api/dict/os-dict-type/types";

class DictEntryApiImpl implements DictEntryApi {
  getKey(model: DictEntryModel): DictEntryKey {
    return {
      dictCode: model.dictCode,
      typeCode: model.typeCode,
    } as DictEntryKey;
  }

  //查询翻页信息数据字典
  findPage(query?: DictEntryReq): AxiosPromise<DictEntryPage> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasEnable = query.enable != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_entry/page/" + tenant,
      method: "get",
      params: query,
    });
  }

  //查询列表信息数据字典
  findList(query?: DictEntryReq): AxiosPromise<DictEntryList> {
    const tenant = useUserStore().user.tenant;
    if (!tenant) return Promise.reject("tenant is empty");
    if (query) {
      query.hasEnable = query.enable != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_entry/list/" + tenant,
      method: "get",
      params: query,
    });
  }

  //查询数据字典
  findById(query: DictEntryKey): AxiosPromise<DictEntryModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url:
        "/v1/dict/dict_entry/id/" +
        tenant +
        "/" +
        query.typeCode +
        "/" +
        query.dictCode,
      method: "get",
    });
  }

  // 创建实体数据字典
  create(data: DictEntryCreateReq): AxiosPromise<DictEntryModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    clearDictCacheAll(data.typeCode);
    return request({
      url: "/v1/dict/dict_entry",
      method: "post",
      data: data,
    });
  }

  //修改实体数据字典
  update(data: DictEntryUpdateReq): AxiosPromise<DictEntryModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    clearDictCacheAll(data.typeCode);
    return request({
      url: "/v1/dict/dict_entry",
      method: "patch",
      data: data,
    });
  }

  //上移下移
  move(key: DictEntryKey, up: boolean): AxiosPromise<boolean> {
    let data: DictEntryMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_entry/move",
      method: "put",
      data: data,
    });
  }

  //启用/禁用
  enable(key: DictEntryKey, enable: boolean): AxiosPromise<boolean> {
    let data: DictEntryEnableKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.enable = enable;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_entry/enable",
      method: "put",
      data: data,
    });
  }

  // 删除数据字典
  delete(query: DictEntryKey): AxiosPromise<DictEntryModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    clearDictCacheAll(query.typeCode);
    return request({
      url: "/v1/dict/dict_entry/id",
      method: "delete",
      params: query,
    });
  }

  // 批量删除数据字典
  batchDelete(keys: DictEntryKey[]): AxiosPromise<number> {
    const data = {
      tenant: useUserStore().user.tenant,
      dictEntryKey: keys,
    } as DictEntryKeyList;
    clearEmpty(data);
    clearDictCacheAll(keys[0].typeCode);
    return request({
      url: "/v1/dict/dict_entry/batch",
      method: "put",
      data: data,
    });
  }

  //下载导入模板数据字典
  downloadTemplate(): AxiosPromise {
    // 检查变量是否定义
    if (typeof dictTypeApi.downloadTemplate === "undefined") {
      return Promise.reject(" dictTypeApi.downloadTemplate is not undefined.");
    }
    return dictTypeApi.downloadTemplate();
  }

  // 导入数据字典
  importExcel(data: DictTypeImportReq): AxiosPromise<string> {
    // 检查变量是否定义
    if (typeof dictTypeApi.importExcel === "undefined") {
      return Promise.reject(" dictTypeApi.importExcel is not undefined.");
    }
    return dictTypeApi.importExcel(data);
  }

  //导出数据字典Excel文件
  exportExcel(queryParams: DictEntryExportReq): AxiosPromise {
    const req = {} as DictTypeExportReq;
    req.req = { typeCode: queryParams.req.typeCode };
    // 检查变量是否定义
    if (typeof dictTypeApi.exportExcel === "undefined") {
      return Promise.reject(" dictTypeApi.exportExcel is not undefined.");
    }
    return dictTypeApi.exportExcel(req);
  }
}

const dictEntryApi = new DictEntryApiImpl();

//清理缓存.
export function clearDictCacheAll(typeCode?: string): void {
  if (typeCode) {
    clearDictCache(typeCode);
    clearDictCache(typeCode, true);
  }
}
//清理缓存.
export function clearDictCache(typeCode: string, enable?: boolean): void {
  const dictKey: string = `dict_${typeCode}_${enable}`;
  sessionStorage.removeItem(dictKey);
}

export { dictEntryApi };
